package com.alibaba.citrus.ots.price.ability.functions.factory.price.process;

import com.alibaba.acm.shaded.com.google.common.collect.Lists;
import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.common.exception.FacadeException;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.bcots.model.dto.BaselineGetOrganizationByIdsRequest;
import com.epoch.app.bcots.model.dto.BaselineGetOrganizationByOutCodesRequest;
import com.epoch.app.bcots.model.dto.OrganizationSDO;
import com.epoch.app.bcots.service.BaseDataService;
import com.epoch.app.otspricecenter.enums.DimensionPropertyEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author hanguo
 * @date 2021/11/19
 * @Description 销售组织/销售部门/销售组
 */
@Component
public class OrganizationProcess extends PriceProcess {
    /**
     * 基础服务
     */
    @Resource
    public BaseDataService baseDataService;


    private static Log log = Log.getLogger(OrganizationProcess.class);
    @Override
    @RepositoryInvoker
    public Map<String, String> obtainCodeAndDescMap(List<String> codeList, boolean innerCode) {
        List<OrganizationSDO> organizationSDOList = obtainSalesOrganizationSDOList(codeList, innerCode);
        if (CollectionUtils.isEmpty(organizationSDOList)) {
            return new HashMap<>();
        }
        if (innerCode) {
            return organizationSDOList.stream().filter(e -> StringUtils.isNotBlank(e.getId())
                    && StringUtils.isNotBlank(e.getName())).collect(Collectors.toMap(OrganizationSDO::
                    getId, OrganizationSDO::getName, (x, y) -> x));
        }
        return organizationSDOList.stream().filter(e -> StringUtils.isNotBlank(e.getOutCode())
                && StringUtils.isNotBlank(e.getName())).collect(Collectors.toMap(OrganizationSDO::
                getOutCode, OrganizationSDO::getName, (x, y) -> x));
    }

    @Override
    @RepositoryInvoker
    public Map<String, String> obtainInnerCodeAndOuterCodeMap(List<String> codeList, boolean innerCode) {
        List<OrganizationSDO> organizationSDOList = obtainSalesOrganizationSDOList(codeList, innerCode);
        if (CollectionUtils.isEmpty(organizationSDOList)) {
            return new HashMap<>();
        }

        if (innerCode) {
            return organizationSDOList.stream().filter(e -> StringUtils.isNotBlank(e.getId())
                    && StringUtils.isNotBlank(e.getOutCode())).collect(Collectors.toMap(OrganizationSDO::
                    getId, OrganizationSDO::getOutCode, (x, y) -> x));
        }
        return organizationSDOList.stream().filter(e -> StringUtils.isNotBlank(e.getId())
                && StringUtils.isNotBlank(e.getOutCode())).collect(Collectors.toMap(OrganizationSDO::
                getOutCode, OrganizationSDO::getId, (x, y) -> x));
    }

    @Override
    public String getPropertyCode() {
        return DimensionPropertyEnum.ORG_SALES_ORGANIZATION_CODE.getKey();
    }

    @RepositoryInvoker
    public List<OrganizationSDO> obtainSalesOrganizationSDOList(List<String> codeList, boolean innerCode) {
        List<OrganizationSDO> organizationSDOList = Lists.newArrayList();

        codeList = dealCodeList(codeList);

        if (Objects.isNull(codeList)) {
            return organizationSDOList;
        }

        Map<String, Object> orgSalesOrganizationCodeResult;
        for (List<String> e : ListUtils.partition(codeList, 15)) {
            if (innerCode) {
                //根据内部编码查询
                BaselineGetOrganizationByIdsRequest baselineGetOrganizationByIdsRequest = new BaselineGetOrganizationByIdsRequest();
                baselineGetOrganizationByIdsRequest.setIds(e);
                baselineGetOrganizationByIdsRequest.setStatus(1);
                orgSalesOrganizationCodeResult = (Map<String, Object>) baseDataService.baselineGetOrganizationByIds(baselineGetOrganizationByIdsRequest);
            } else {
                BaselineGetOrganizationByOutCodesRequest baselineGetOrganizationByOutCodesRequest = new BaselineGetOrganizationByOutCodesRequest();
                baselineGetOrganizationByOutCodesRequest.setOutCodes(e);
                baselineGetOrganizationByOutCodesRequest.setStatus(1);
                //根据外部编码查询
                orgSalesOrganizationCodeResult = (Map<String, Object>) baseDataService.baselineGetOrganizationByOutCodes(baselineGetOrganizationByOutCodesRequest);
            }
            if (Objects.isNull(orgSalesOrganizationCodeResult)) {
                log.error("接口调用异常，请排查 com.epoch.app.bcots.service.BaseDataService.baselineGetOrganizationByIds()");
                throw new FacadeException("OTS-08-003-01-16-001");
            }
            List<Map<String, Object>> orgSalesOrganizationCodeResultList = (List<Map<String, Object>>) orgSalesOrganizationCodeResult.get("result");
            if (CollectionUtils.isNotEmpty(orgSalesOrganizationCodeResultList)) {
                organizationSDOList.addAll(orgSalesOrganizationCodeResultList.stream().
                        map(item -> JSONObject.parseObject(JSONObject.toJSONString(item.get("organizationSDO")), OrganizationSDO.class)).
                        collect(Collectors.toList()));
            }

        }
        return organizationSDOList;


    }
}
